package com.yocard.yxyd.ui.activity.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.gyf.barlibrary.ImmersionBar;
import com.yocard.yxyd.R;
import com.yocard.yxyd.ui.view.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created on 2018/12/10.
 * 无MVP基类Activity
 */
public abstract class SimpleActivity extends AppCompatActivity {
    protected Activity mContext;
    private Unbinder mUnBinder;
    protected LoadingDialog loadingDailog;
    protected LoadingDialog.Builder loadBuilder;
    long lastClickTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        mUnBinder = ButterKnife.bind(this);
        ImmersionBar.with(this).navigationBarColor(R.color.white).navigationBarColorInt(Color.BLACK)
                .autoDarkModeEnable(true, 0.2f).init();
        mContext = this;
        onViewCreated();
        highApiEffects();
        initEventAndData();
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void highApiEffects() {
        getWindow().getDecorView().setFitsSystemWindows(true);
        //透明状态栏 @顶部
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏 @底部    这一句不要加，目的是防止沉浸式状态栏和部分底部自带虚拟按键的手机（比如华为）发生冲突，注释掉就好了
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    protected void onViewCreated() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnBinder.unbind();
        ImmersionBar.with(this).destroy();
        dismisDialog();
    }

    public void showLoadingDialog() {
        showLoadingDialog("加载中...");
    }

    public void showLoadingDialog(String msg) {
        if (loadingDailog == null) {
            loadBuilder = new LoadingDialog.Builder(this)
                    .setMessage(msg)
                    .setCancelable(true)
                    .setCancelOutside(false);
            loadingDailog = loadBuilder.create();
            loadingDailog.show();
        } else {
            if (!loadingDailog.isShowing()) {
                loadingDailog.show();
            }
        }
    }

    public void dismisDialog() {
        if (loadingDailog != null && loadingDailog.isShowing()) {
            loadingDailog.cancel();
        }
    }

    public boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 400) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


    protected abstract int getLayout();

    protected abstract void initEventAndData();
}
